﻿using Jive.Helpers;
using Jive.Network;
using Jive.User;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace Jive.Settings
{
    public static class StaticSettings
    {
        //private static StaticSettings _instance;

        //public static StaticSettings GetInstance()
        //{
        //    if (_instance == null)
        //        return _instance = new StaticSettings();
        //    else
        //        return _instance;
        //}
        //private static bool? _isInDesignMode;

//        /// <summary>
//        /// Gets a value indicating whether the control is in design mode (running in Blend
//        /// or Visual Studio).
//        /// </summary>
//        public static bool IsInDesignMode
//        {
//            get
//            {
//                if (!_isInDesignMode.HasValue)
//                {
//#if SILVERLIGHT
//                    _isInDesignMode = DesignerProperties.IsInDesignTool;
//#else
//            var prop = DesignerProperties.IsInDesignModeProperty;
//            _isInDesignMode
//                = (bool)DependencyPropertyDescriptor
//                .FromProperty(prop, typeof(FrameworkElement))
//                .Metadata.DefaultValue;
//#endif
//                }

//                return _isInDesignMode.Value;
//            }
//        }
        public static string CustomEmoticonFolder = "CustomEmoticons";

        public static string CustomContactEmoticonFolder = "Custom_Contact_Emoticons";

        public static bool IsWifiEnabled
        {
            get
            {
                //return Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsWiFiEnabled;
                var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
                var interfaceType = profile.NetworkAdapter.IanaInterfaceType;

                // 71 is WiFi & 6 is Ethernet(LAN)
                if (interfaceType == 71 || interfaceType == 6)
                {
                    return true;
                }
                //// 243 & 244 is 3G/Mobile
                //else if (interfaceType == 243 || interfaceType == 244)
                //{
                //    return false;
                //}
                return false;
            }
        }
        public static int MaxMessageToShowCount
        {
            get { return 50; }
        }
        public static int ServerPort
        {
#if DEBUG
            get { return 36411; }
#else
            get { return 36410; }
#endif

        }
        public static SolidColorBrush MaroonBrush
        {
            get
            {
                return (SolidColorBrush)Application.Current.Resources["MaroonBrush"];
            }
        }
        public static Color MaroonColor
        {
            get
            {
                //if (!_maroonColor.HasValue)
                //{
                //    _maroonColor = StaticMethods.ColorFromHex("FF800000");
                //}
                if (MaroonBrush != null && MaroonBrush.Color != null)
                    return MaroonBrush.Color;
                else
                    return Colors.Brown;
            }
        }
        public static SolidColorBrush AccentBrush
        {
            get
            {
                return (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
            }
        }
        public static SolidColorBrush DisabledBrush
        {
            get
            {
                return (SolidColorBrush)Application.Current.Resources["JiveDisabledBrush"];
            }
        }
        public static bool DarkThemeUsed
        {
            get
            {
                return Visibility.Visible == (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            }
        }
        public static SolidColorBrush BackgroundBrush
        {
            get
            {
                return (SolidColorBrush)Application.Current.Resources["JiveBackgroundBrush"];// Methods.ColorFromHex("#FFE4E4E4");// DarkThemeUsed ? Colors.Black : Colors.White;
            }
        }

        private static Dictionary<string[], string> _defaultEmoticons;
        public static Dictionary<string[], string> DefaultEmoticons
        {
            get { return _defaultEmoticons ?? (_defaultEmoticons = MakeDefaultEmoticonList()); }
        }

        //private  Dictionary<string, Dictionary<string, string>> _customContactEmoticons;
        public static Dictionary<string, Dictionary<string, string>> CustomContactEmoticons
        {
            get
            {
                return AppSettings.AppSettingsInstance.CustomContactEmoticons;
            }
            set
            {
                AppSettings.AppSettingsInstance.CustomContactEmoticons = value;
            }
        }

        public static Dictionary<string, string> CustomEmoticons
        {
            get { return AppSettings.AppSettingsInstance.CustomEmoticons; }
            set
            {
                AppSettings.AppSettingsInstance.CustomEmoticons = value;
            }
        }
        public static Dictionary<string, string> GetEmoticons(string UserID, bool includeDefault, bool includeCustom)
        {
            Dictionary<string, string> allEmoticons = new Dictionary<string, string>();// { { "<line>", "line.gif" }, { "<spacer>", "spacer.gif" } };
            if (includeCustom && !String.IsNullOrEmpty(UserID) && AppSettings.AppSettingsInstance.CustomContactEmoticons != null)
            {
                Dictionary<string, string> ccEmoticons;
                AppSettings.AppSettingsInstance.CustomContactEmoticons.TryGetValue(UserID, out ccEmoticons);

                if (ccEmoticons != null && !UserID.Equals(Client.ClientInstance.CurrentUserInfo.UserID))
                {
                    allEmoticons.AddRange(ccEmoticons);
                }
                else if (UserID.Equals(Client.ClientInstance.CurrentUserInfo.UserID))
                {
                    allEmoticons.AddRange(CustomEmoticons);
                }
            }
            if (includeDefault)
            {
                //AllEmoticons.AddRange(DefaultEmoticons);

                foreach (KeyValuePair<string[], string> kvp in DefaultEmoticons)
                {
                    foreach (string s in kvp.Key)
                    {
                        string key = s.ToLower();
                        if (!allEmoticons.ContainsKey(key))
                            allEmoticons.Add(key, kvp.Value);
                        if (!allEmoticons.ContainsKey(s))
                            allEmoticons.Add(s, kvp.Value);
                    }
                }
            }
            return allEmoticons;
        }
        private static Dictionary<string[], string> MakeDefaultEmoticonList()
        {
            Dictionary<string[], string> defaultEmoticons = new Dictionary<string[], string>
                {
                    {new[]{":)",":-)"}, "Smile.png"},
                    {new[]{":D",":-D"}, "Open_mouthed.png"},
                    {new[]{";)",";-)"}, "Wink_smile.png"},
                    {new[]{":O",":-O"}, "Surprised.png"},
                    {new[]{":P"}, "Tongue_out.png"},
                    {new[]{"(H)"}, "hot.png"},
                    {new[]{":@"}, "Angry.png"},
                    {new[]{":S",":-S"}, "Confused.png"},
                    {new[]{":$"}, "Embarrassed.png"},
                    {new[]{":(",":-("}, "Sad.png"},
                    {new[]{":'("}, "Crying.png"},
                    {new[]{":|"}, "Disappointed.png"},
                    {new[]{"(A)"}, "Angel.png"},
                    {new[]{"8o|"}, "Baring_teeth.png"},
                    {new[]{"8-|"}, "Nerd.png"},
                    {new[]{"+o("}, "Sick.png"},
                    {new[]{"|-)"}, "Sleepy.png"},
                    {new[]{"<:o)"}, "Party.png"},
                    {new[]{":^)"}, "I_dont_know.png"},
                    {new[]{":-#"}, "Not_Telling.png"},
                    {new[]{":-*"}, "Secret_telling.png"},
                    {new[]{"^o)"}, "Sarcastic.png"},
                    {new[]{"8-)"}, "Eye_rolling.png"},
                    {new[]{"(L)"}, "Red_heart.png"},
                    {new[]{"(U)"}, "Broken_heart.png"},
                    {new[]{"({)"}, "Left_hug.png"},
                    {new[]{"(})"}, "Right_hug.png"},
                    {new[]{"(bah)"}, "Sheep.png"},
                    {new[]{"*-)"}, "Thinking.png"},
                    {new[]{"(B)"}, "Beer.png"},
                    {new[]{"(@)"}, "Cat.png"},
                    {new[]{"(D)"}, "Cocktail.png"},
                    {new[]{"(C)"}, "Coffee.png"},
                    {new[]{"(6)"}, "Devil.png"}
                };

            return defaultEmoticons;
        }
    }
}
